package graph.unweighted.basic;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.TreeSet;
/**
 * 
 * @Title: AdjacentList.java 
 * @Package graph 
 * @Description: 底层基于TreeSet的邻接表
 * @author CandyWall   
 * @date 2021年1月14日 下午12:35:12 
 * @version V1.0
 */
public class AdjacentSet {
    private int vertex_num; // 当前图中的顶点数
    private int edge_num;   // 当前图中的边数
    private TreeSet<Integer>[] adj;    // 邻接矩阵
    /*public AdjacentMatrix(String filename) {
        try(BufferedReader reader = new BufferedReader(new FileReader(filename))) {
            // 读取顶点数和边数
            String line = reader.readLine();
            String[] params = line.split(" ");
            vertex_num = Integer.parseInt(params[0]);
            edge_num = Integer.parseInt(params[1]);
            // 初始化邻接矩阵
            adj = new int[vertex_num][vertex_num];
            for(int i = 0; i < vertex_num; i++) {
                for(int j = 0; j < vertex_num; j++) {
                    adj[i][j] = 0;
                }
            }
            // 读取边各个边，存到邻接矩阵中
            while((line = reader.readLine()) != null) {
                params = line.split(" ");
                int i = Integer.parseInt(params[0]);
                int j = Integer.parseInt(params[1]);
                adj[i][j] = 1;
                adj[j][i] = 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
    // 打印邻接矩阵
    /*private void printAdj() {
        // 打印列下标
        System.out.print("  ");
        for(int i = 0; i < vertex_num; i++) {
            System.out.print(i + " ");
        }
        System.out.println();
        for(int i = 0; i < vertex_num; i++) {
            // 打印行下标
            System.out.print(i + " ");
            for(int j = 0; j < vertex_num; j++) {
                System.out.print(adj[i][j] + " ");
            }
            System.out.println();
        }
    }*/
    
    /**
     * 从文件中加载一个图
     * @param filename
     */
    public AdjacentSet(String filename) {
        try(Scanner scanner = new Scanner(new File(filename))) {
            // 读取顶点数
            vertex_num = scanner.nextInt();
            if(vertex_num < 0) {
                throw new IllegalArgumentException("顶点数必须是非负数！");
            }
            // 读取边数
            edge_num = scanner.nextInt();
            if(edge_num < 0) {
                throw new IllegalArgumentException("边数必须是非负数！");
            }
            // 初始化邻接表
            adj = new TreeSet[vertex_num];
            for(int i = 0; i < vertex_num; i++) {
                adj[i] = new TreeSet<>();
            }
            // 读取边各个边
            for(int i = 0; i < edge_num; i++) {
                int a = scanner.nextInt();
                validateVertex(a);
                int b = scanner.nextInt();
                validateVertex(b);
                if(a == b) {
                    throw new IllegalArgumentException("检测到了自环边！");
                }
                if(adj[a].contains(b)) {
                    throw new IllegalArgumentException("检测到了平行边！");
                }
                adj[a].add(b);
                adj[b].add(a);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchElementException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("边数少于指定的值！");
        }
    }
    
    /**
     *  判断顶点是否合法
     * @param v
     */
    private void validateVertex(int v) {
        if(v < 0 || v >= vertex_num) {
            throw new IllegalArgumentException("顶点" + v + "不合法！");
        }
    }
    
    /**
     *  获取顶点数
     * @return
     */
    public int getVertex_num() {
        return vertex_num;
    }

    /**
     *  获取边数
     * @return
     */
    public int getEdge_num() {
        return edge_num;
    }
    
    /**
     * 判断顶点1和顶点2之间是否有一条边
     * @param v
     * @param w
     * @return
     */
    public boolean hasEdge(int v, int w) {
        validateVertex(v);
        validateVertex(w);
        return adj[v].contains(w);
    }
    
    /**
     * 返回和某个顶点相邻的所有顶点
     * @param v
     * @return
     */
    public Iterator<Integer> adjacentVertexes(int v) {
        validateVertex(v);
        return adj[v].iterator();
    }
    
    /**
     * 返回一个顶点的度
     * @param v
     * @return
     */
    public int getDegree(int v) {
        validateVertex(v);
        return adj[v].size();
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(String.format("顶点数  = %d\n边数 = %d\n", vertex_num, edge_num));
        sb.append("  ");
        for(int i = 0; i < vertex_num; i++) {
            sb.append(i + " ");
        }
        sb.append('\n');
        for(int v = 0; v < vertex_num; v++) {
            // 打印行下标
            sb.append(v + " ");
            for(int w : adj[v]) {
                sb.append(w + " ");
            }
            sb.append('\n');
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        AdjacentSet adjacentSet = new AdjacentSet("testData/graph/graph.txt");
        // 打印邻接矩阵，验证图是否被正确保存
        //adjacentMatrix.printAdj();
        System.out.println(adjacentSet);
        System.out.print("1的邻边有：");
        Iterator<Integer> iter = adjacentSet.adjacentVertexes(1);
        while(iter.hasNext()) {
            System.out.print(iter.next() + " ");
        }
    }
}
